package com.github.yoojia.flow.impl;

import com.github.yoojia.flow.Context;
import com.github.yoojia.flow.Event;
import com.github.yoojia.flow.EventInterceptor;
import com.github.yoojia.flow.ScriptingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.ScriptException;

/**
 * JavaScript事件拦截器
 * @author 陈小锅 (yoojiachen@gmail.com)
 * @since 1.0.0
 */
public class ScriptingEventInterceptor extends ScriptingContext implements EventInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingEventInterceptor.class);

    @Override
    public boolean onInterceptEvent(Context context, Event event) {
        try {
            return invokeFunction("onInterceptEvent", context, event);
        } catch (ScriptException e) {
            LOGGER.error("Error when invoke interceptor script", e);
        } catch (NoSuchMethodException e) {
            LOGGER.error("Script file, onInterceptEvent(Context, Event) function not found", e);
        }
        return false;
    }
}
